#include <stdio.h>
#include <stdlib.h>
#include "liste_chaine.h"

/////////////////////////////////////////////////////////
// Fonction initiallisant une structure "Vector" a 0
// Entrees : vector - pointeur vers une structure Vector a
//                    initialliser
//
void ini_Vector(Vector* vector)
{
   vector->firstCell = NULL;
   vector->length = 0;
}



/////////////////////////////////////////////////////////
// Fonction transformant un tableau en vecteur
//
void table2vect(int array[],int length_array,Vector* vector)
{
   int i;
   Cell *actualCell = NULL;
   
   vector->length = length_array;
   vector->firstCell = malloc(sizeof(Cell));
   actualCell = vector->firstCell;
   actualCell->value = array[0];
   actualCell->next = NULL;
   
   for(i=1;i<length_array;i++)
   {
      actualCell->next = malloc(sizeof(Cell));
      actualCell = actualCell->next;
      actualCell->value = array[i];
      actualCell->next = NULL;
   }
   
   
}


/////////////////////////////////////////////////////////
//Fonction permettant d'ajouter une cellule au vecteur
//
void add_cell(Vector *vector, int pos, int value)
{
   Cell *newCell = NULL;
   Cell *activeCell = NULL, *prevCell = NULL, *nextCell = NULL;
   int i;
   
   pos = (pos>vector->length)? vector->length : pos;
   pos = (pos>0)? pos : 0;
   
   newCell = malloc(sizeof(Cell));
   
   
   if (vector->length==0)
   {
      vector->firstCell = newCell;
      newCell->next = NULL;
   }
   else if (pos==0)
   {
      nextCell = vector->firstCell;
      vector->firstCell = newCell;
      
      newCell->next = nextCell;
   }
   else
   {
      activeCell = vector->firstCell;
      nextCell = activeCell->next;
      
      for (i=1;i<=pos;i++)
      {
         prevCell = activeCell;
         activeCell = nextCell;
         
         if (i!=vector->length)
         {
            nextCell = activeCell->next;
         }
      }
      
      prevCell->next = newCell;
      
      newCell->next = activeCell;
      
   }
      
   vector->length += 1;
   newCell->value = value;
      
      
}


/////////////////////////////////////////////////////////
// Fonction permettant d'enlever une cellule au vecteur
//
void rm_cell(Vector *vector, int pos)
{
   Cell *activeCell = NULL, *nextCell = NULL, *prevCell = NULL;
   int i;
   
   pos = (pos>=vector->length)? (vector->length-1) : pos;
   pos = (pos>0)? pos : 0;
   
   
   if (pos == 0)
   {
      activeCell = vector->firstCell;
      nextCell = activeCell->next;
      
      vector->firstCell = nextCell;
   }
   else
   {
      activeCell = vector->firstCell;
      nextCell = activeCell->next;
      
      for (i=1;i==pos;i++)
      {
         prevCell = activeCell;
         activeCell = nextCell;
         nextCell = activeCell->next;
      }
      
      prevCell->next = nextCell;
   }
   
   free(activeCell);
   
   vector->length -= 1;
}

/////////////////////////////////////////////////////////
// Fonction permettant d'obtenir un pointeur vers la 
//    valeur d'une cellule du vecteur
//
int* ite_Vect(Vector *vector, int pos)
{
   Cell *activeCell = NULL;
   int i;
   
   pos = (pos>=vector->length)? (vector->length-1) : pos;
   pos = (pos>0)? pos : 0;
   
   activeCell = vector->firstCell;
   
   for (i=1;i<=pos;i++)
   {
      activeCell = activeCell->next;
   }
   
   return &(activeCell->value);
}



/////////////////////////////////////////////////////////
// Fonction affichant le vecteur
//
void print_Vect(Vector* vector)
{
   Cell *activeCell = NULL;
   int i;
   
   activeCell = vector->firstCell;
   printf("[");
   if (activeCell != NULL)
   {
      printf("%i",activeCell->value);
      while (activeCell->next != NULL)
      {
        activeCell = activeCell->next;
        printf(",%i",activeCell->value);
      }
   }
   printf("] \n");
   
}

/////////////////////////////////////////////////////////
// Fonction supprimant toutes les cellules du vecteur
//
void del_Vect(Vector* vector)
{
   Cell *activeCell = NULL, *nextCell = NULL;
   
   activeCell = vector->firstCell;
   vector->firstCell = NULL;
   vector->length = 0;
   
   while (activeCell != NULL)
   {
      nextCell = activeCell->next;
      free(activeCell);
      activeCell = nextCell;
   }
}









